{{- /*
@param {string} (positional parameter 0) Title of the Wikipedia article.
@param {string} (positional parameter 1) Language of the Wikipedia article.

@returns {template.HTML}

@examples

  {{< wiki "Gravitational singularity" "en" >}}
*/ -}}

{{- if (.Get 0) -}}

    {{ $title := (.Get 0) }}
    {{ $lang := ((.Get 1) | default "ru") }}
    {{ $ts := (now.Unix) }}
    {{ $headers := (dict
    "User-Agent" (site.Params.api.user_agent)
    ) }}

    {{ $api := "format=json&action=query" }}
    {{ $extracts := "prop=extracts&exintro&explaintext" }}
    {{ $data := (getJSON "https://" ($lang) ".wikipedia.org/w/api.php?" ($api) "&" ($extracts) "&redirects=1&titles=" (($title) | urlquery) "&ts=" ($ts) ($headers)) }}

    {{ range ($data.query.pages) }}
      <div class="shortcode shortcode-{{ ($.Name) }} mb-3">
        <div class="card">
          <div class="card-header">
            <div class="d-flex align-items-center">
              <div class="flex-shrink-0">
                <i class="fab fa-wikipedia-w fa-fw"></i>
              </div>
              <div class="flex-grow-1 mx-2">
                <h5 class="mb-0">{{ (.title) }}</h5>
              </div>
              <div class="flex-shrink-0">
                <ul class="list-inline mb-0">
                  <li class="list-inline-item" data-bs-tooltip data-bs-title='{{ (i18n "sc.wiki.link") }}'>
                      {{ $url := (printf "https://%s.wikipedia.org/wiki/%s" ($lang) (replace (.title) " " "_")) }}
                    <a class="text-body-secondary" href="{{ ($url) }}"
                       target="_blank" rel="noopener nofollow"
                       role="button" aria-label='{{ (i18n "sc.wiki.link") }}'>
                      <i class="fas fa-arrow-up-right-from-square fa-fw"></i>
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </div>
          <div class="card-body">
            <div class="node-body">{{ ((replaceRE `(.*)` "<p>$1</p>" (.extract)) | safeHTML) }}</div>
          </div>
        </div>
      </div>
    {{ end }}

{{- else -}}

    {{ (partial "alerts/no_params" .) }}

{{- end -}}
